<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">token管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>token管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
        所属应用：<select id="token_clients" lay-filter="token_clients"></select>
        &emsp;搜索：<input id="tokens-edit-search" class="layui-input search-input" type="text" placeholder="输入用户名"/>&emsp;
        <button id="tokens-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="tokens-table" lay-filter="tokens-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tokens-table-bar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'],function () {
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let util = layui.util;
        let admin = layui.admin;
        let form = layui.form;

        // 渲染表格
        table.render({
            elem: '#tokens-table',
            url: config.base_server + 'api-uaa/tokens',
            method: 'GET',
            headers:{'Authorization': 'Bearer ' + config.getToken().access_token},
            where: {tenantId: config.clientId},
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'tokenValue',width:300, sort: true, title: 'token'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.expiration);
                    }, width:180, title: '到期时间'
                },
                {field: 'username',width:180, sort: true, title: '用户名'},
                {field: 'grantType',width:180, sort: true, title: '授权类型'},
                {field: 'clientId',width:180, sort: true, title: '所属应用'},
                {field: 'accountType',width:180, sort: true, title: '账号类型'},
                {align: 'center',width:100, toolbar: '#tokens-table-bar', title: '操作'}
            ]]
        });

        // 获取应用列表
        layer.load(2);
        admin.req('api-uaa/clients/all', {}, function (data) {
            layer.closeAll('loading');
            if (0 === data.resp_code) {
                let selected = false;
                $.each(data.datas,function(index,item){
                    if (config.clientId === item.clientId) {
                        selected = true;
                    } else {
                        selected = false;
                    }
                    //往下拉菜单里添加元素
                    $('#token_clients').append(new Option(item.clientName, item.clientId, false, selected));
                })
                form.render();
            } else {
                layer.msg(data.resp_msg, {icon: 2, time: 500});
            }
        }, 'GET');

        // 工具条点击事件
        table.on('tool(tokens-table)', function (obj) {
            if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        // 删除
        let doDelete = function (obj) {
            layer.confirm('确定要删除吗？', function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('api-uaa/oauth/remove/token?token=' + obj.data.tokenValue, {}, function (data) {
                    layer.closeAll('loading');
                    layer.msg('成功', {icon: 1, time: 500});
                    obj.del();
                }, 'DELETE');
            });
        };

        // 搜索按钮点击事件
        $('#tokens-btn-search').click(function () {
            var key = $('#tokens-edit-search').val();
            table.reload('tokens-table', {where: {username: key, tenantId: $('#token_clients').val()}});
        });

        // 应用下来框点击事件
        form.on('select(token_clients)', function(data){
            table.reload('tokens-table', {where: {tenantId: data.value}});
        });
    });
</script>